package listener;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.InputStream;

@WebListener
public class TomcatStartListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent e) {
        InputStream in = TomcatStartListener.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory ssf = sessionFactoryBuilder.build(in);
        e.getServletContext().setAttribute("SqlSessionFactory",ssf);
    }

    @Override
    public void contextDestroyed(ServletContextEvent e) {
        e.getServletContext().removeAttribute("SqlSessionFactory");
    }
}
